package com.beidouapp.service.impl;

import com.beidouapp.common.contant.SecurityContant;
import com.beidouapp.common.enums.YesNoEnum;
import com.beidouapp.dao.OrganizationAndRoleDao;
import com.beidouapp.dao.OrganizationDao;
import com.beidouapp.domain.pojo.Organization;
import com.beidouapp.domain.pojo.OrganizationAndRole;
import com.beidouapp.exception.BusinessException;
import com.beidouapp.security.model.SessionUser;
import com.beidouapp.service.OrganizationService;
import com.beidouapp.translator.TranslatorManager;
import com.beidouapp.utils.UIUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private OrganizationAndRoleDao organizationAndRoleDao;

    @Autowired
    private TranslatorManager translatorManager;

    @Override
    public List<Organization> findOrganizationListWithPageByMap(Integer offset, Integer limit, Map<String, Object> params) {
        params.put(SecurityContant.IS_DEL,YesNoEnum.No.getCode());
        List<Organization> list = organizationDao.findEntryListByMap(params, offset, limit);
        return translatorManager.translate(list);
    }

    @Override
    public Long findOrganizationCountByMap(Map<String, Object> params) {
        params.put(SecurityContant.IS_DEL,YesNoEnum.No.getCode());
        return organizationDao.findEntryCountByMap(params);
    }

    @Override
    public List<Organization> findOrganizationList(Organization organization) {
        List<Organization> list = organizationDao.findEntryList(organization);
        return translatorManager.translate(list);
    }

    @Override
    public Organization getOrganizationById(Long orgId){
        Organization org = organizationDao.getEntryByID(orgId);
        if(org == null){
            return null;
        }
        return translatorManager.translate(org);
    }

    @Override
    public Integer insertOrganization(Organization organization){
        if(organization == null || StringUtils.isEmpty(organization.getCode())){
            throw new BusinessException("新增组织机构时，机构或机构编码不能为空.");
        }
        Organization o = new Organization();
        o.setCode(organization.getCode());
        List<Organization> list = organizationDao.findEntryList(o);
        if(!CollectionUtils.isEmpty(list)){
            throw new BusinessException("新增组织机构时，机构编码重复.");
        }

        organization.setIsDel(YesNoEnum.No.getCode());
        organization.setEnable("1");
        return organizationDao.addEntry(organization);
    }

    @Override
    public Integer removeOrganizationBatch(List<Long> ids){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("list", ids);
        return organizationDao.removeOrganizationBatch(params);
    }

    @Override
    public List<OrganizationAndRole> findOrganizationAndRoleList(Long orgId, Long roleId){
        OrganizationAndRole organizationAndRole = new OrganizationAndRole();
        organizationAndRole.setOrgId(orgId);
        organizationAndRole.setRoleId(roleId);
        List<OrganizationAndRole> list = organizationAndRoleDao.findEntryList(organizationAndRole);
        return list;
    }

    @Override
    public Integer modifyOrganization(Organization organization){
        if(organization == null){
            throw new BusinessException("修改的组织机构不能为空!");
        }
        organization.setUpdateBy(UIUtils.getSessionUser().getUserId());
        organization.setUpdateTime(new Date());
        return organizationDao.modifyEntry(organization);
    }

}
